﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports DevExpress.Utils

Public Class FormRk
    Private en As entity_lx_ck
    Dim dtPgdMes As DataTable
    Dim objCA As New clsLXCK
    Dim objProdInfo As New clsProdInfo
    Dim objProdSpec As New clsProdSpec
    Dim objMstProd As New clsMstProd
    Dim objPgdMes As New clsPgdMes
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim httpHelper As New HttpHelper
    Public usr As String
    Public PgdMesSysId As String
    Dim warehousecode As String
    Dim b_mes As Boolean = False
    Private Sub FormRk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        setEmptyLOC()
        txtProdId.Focus()
    End Sub

    Private Sub setEmptyLOC()
        'Dim objMstList As New clsMstList
        'Dim dt As DataTable
        'loc.Items.Clear()
        'dt = objMstList.getEmptyLoc("3")

        'If dt.Rows.Count > 0 Then
        '    For i As Integer = 0 To dt.Rows.Count - 1
        '        loc.Items.Add(dt.Rows(i)("mst_value"))
        '    Next
        'End If
    End Sub

    Private Sub txtProdId_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProdId.KeyPress
        If e.KeyChar = vbCr Then
            Search()
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Search()
    End Sub
    Private Sub Search()
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) = False Then
            Dim dtt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim, "10") '硫化出库后的状态
            If dtt.Rows.Count > 0 Then
                Dim dr As DataRow = dtt.Rows(0)
                txt_prod_spec.Text = dr("prod_spec_id")
                Dim dtProdSpecName As DataTable = objProdSpec.selectProdSpec(dr("prod_spec_id"))
                txt_prod_name.Text = dtProdSpecName.Rows(0)("prod_spec_name")
                txtProdSpec.Text = dtProdSpecName.Rows(0)("prod_spec")
                txt_design_area.Text = dtProdSpecName.Rows(0)("design_area")
                Dim dtMstProd As DataTable = objMstProd.getList("力学性能支座II", "II-" & dtProdSpecName.Rows(0)("prod_spec"))
                dtMstProd.Columns.Add("model_name")
                If dtMstProd.Rows.Count > 0 Then
                    For Each row As DataRow In dtMstProd.Rows
                        If row("model").ToString().Contains("A") Then
                            row("model_name") = "A区"
                        End If
                        If row("model").ToString().Contains("B") Then
                            row("model_name") = "B区"
                        End If
                        If row("model").ToString().Contains("C") Then
                            row("model_name") = "C区"
                        End If
                        If row("model").ToString().Contains("D") Then
                            row("model_name") = "D区"
                        End If
                        If row("model").ToString().Contains("E") Then
                            row("model_name") = "E区"
                        End If
                    Next
                End If
                area.DisplayMember = "model_name"
                area.ValueMember = "material_code"
                area.DataSource = dtMstProd
                If dtMstProd.Rows.Count > 0 Then
                    area.SelectedIndex = 0
                End If
                area.Text = txt_design_area.Text
                dtPgdMes = objPgdMes.getListBySysId(PgdMesSysId)
                If dtPgdMes.Rows(0)("support_model").ToString().Equals(txt_prod_spec.Text) = False Then
                    MsgBox("当前标签号与生产计划不匹配", vbYes, "提示")
                End If
            Else
                MsgBox("未检索到此生产编号", vbYes, "提示")
            End If
        End If
    End Sub
    Private Sub btnRK_Click(sender As Object, e As EventArgs) Handles btnRk.Click
        If String.IsNullOrEmpty(txtProdId.Text.Trim()) Then
            Exit Sub
        End If
        If dtPgdMes.Rows(0)("support_model").ToString().Equals(txt_prod_spec.Text.Trim()) = False Then
            MsgBox("当前标签号与生产计划不匹配", vbYes, "提示")
            Exit Sub
        End If
        If String.IsNullOrEmpty(area.Text.Trim()) Then
            MsgBox("未获取到可入库库区信息，请联系系统维护人员！", vbYes, "提示")
            Exit Sub
        End If
        Dim dtxt As DataTable = objProdInfo.getProdInfoById(txtProdId.Text.Trim(), "10")
        If dtxt.Rows.Count > 0 Then
            Dim dtt As DataTable = objCA.getListByProdId(txtProdId.Text.Trim(), "0")
            If dtt.Rows.Count = 0 Then
                setEN(PgdMesSysId)
                If objCA.intoCK(en) = -2 Then
                    MsgBox("入库失败", vbYes, "提示")
                Else
                    If b_mes Then
                        Dim b_PgdMesProjectCode As String = dtPgdMes.Rows(0)("mes_project_code").ToString()
                        Dim b_PgdMes_gx As String = dtPgdMes.Rows(0)("type_value").ToString()
                        Dim rtn_mes_bg As String
                        If txt_design_area.Text.Trim().Equals(area.Text) Then
                            rtn_mes_bg = mesbgOk("1", b_PgdMes_gx, b_PgdMesProjectCode)
                        Else
                            rtn_mes_bg = mesbgNg("1", b_PgdMes_gx, b_PgdMesProjectCode, area.SelectedValue.ToString())
                        End If
                        Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
                        Dim code As String = jo("code").ToString() '输出0
                        Dim msg As String = jo("msg").ToString() '输出操作成功
                        objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", usr, txtProdId.Text.Trim(), "4", code)
                        Dim rnt_mes_rk As String = mesRk("1", warehousecode,
                                                        dtPgdMes.Rows(0)("type_value").ToString(),
                                                        dtPgdMes.Rows(0)("mes_project_code").ToString())
                        Dim jo_rk As JObject = JsonConvert.DeserializeObject(rnt_mes_rk)
                        Dim code_rk As String = jo_rk("code").ToString() '输出0
                        Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功
                        objCA.update_IntoCK_MES(txtProdId.Text.Trim(), "0", code_rk)
                        If code.Equals("0") And code_rk.Equals("0") Then
                            MsgBox("入库成功，MES报工成功，MES入库成功", vbYes, "提示")
                            clear()
                        Else
                            If code.Equals("0") Or code_rk.Equals("0") Then
                                If code.Equals("0") Then
                                    MsgBox("入库成功，MES报工成功，MES入库失败：" & msg_rk, vbYes, "提示")
                                End If
                                If code_rk.Equals("0") Then
                                    MsgBox("入库成功，MES报工失败：" & msg & "；MES入库成功"， vbYes, "提示")
                                End If
                            Else
                                MsgBox("入库成功，" & "MES报工失败：" & msg & "；MES入库失败：" & msg_rk， vbYes, "提示")
                            End If
                        End If
                    Else
                        objPgdMesWorkReport.insert("ok", PgdMesSysId, "1", usr, txtProdId.Text.Trim(), "4", String.Empty)
                        MsgBox("入库成功", vbYes, "提示")
                    End If
                End If
            Else
                MsgBox("该标签号已入库", vbYes, "提示")
            End If
        Else
            MsgBox("未查询到硫化出库的此生产编号信息", vbYes, "提示")
        End If
    End Sub

    Public Sub clear()
        txtProdId.Text = String.Empty
        txt_prod_spec.Text = String.Empty
        txt_prod_name.Text = String.Empty
        txtProdSpec.Text = String.Empty
        txt_design_area.Text = String.Empty
        area.Text = String.Empty
        area.DataSource = New DataTable()
        loc.Text = String.Empty
        setEmptyLOC()
        txtProdId.Focus()
    End Sub

    Public Sub setEN(b_pgd_mes_sys_id As String)
        en = New entity_lx_ck
        With en
            .factory_id = "TS"
            .prod_id = txtProdId.Text.Trim()
            .area = area.Text.Trim()
            .location = loc.Text.Trim()
            .b_pgd_mes_sys_id = b_pgd_mes_sys_id
            .status = "0"
            .into_ope = usr
        End With
    End Sub

    'mes报工Ok
    Private Function mesbgOk(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "力学" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    'mes报工Ng
    Private Function mesbgNg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String, lx_material_code As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "力学" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """," '不合格原因
        params &= """unqualifiedProductorname"":""" & lx_material_code & """," '力学偏曲不合格数量编码
        params &= """unqualifiedMapname"":""" & warehousecode & """" '力学偏曲不合格数量入库仓库
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    'mes入库
    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "力学" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS0901""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim b_PgdMesProjectCode As String = "GⅡ型700支座-结构七-S-CL-001"
        Dim b_PgdMes_gx As String = "02.03.02.038"
        Dim warehousecode As String = "质监检测库"
        Dim rnt_mes_bg As String = mesbgOk("1", b_PgdMes_gx, b_PgdMesProjectCode)
        Dim xdfd As String = rnt_mes_bg
        'mesbgNg("1", b_PgdMes_gx, b_PgdMesProjectCode, "")
        Dim rnt_mes_rk As String = mesRk("1", warehousecode,
                                                        b_PgdMes_gx,
                                                        b_PgdMesProjectCode)
        Dim jo_rk As JObject = JsonConvert.DeserializeObject(rnt_mes_rk)
        Dim code_rk As String = jo_rk("code").ToString() '输出0
        Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim b_PgdMesProjectCode As String = "GⅡ型800支座-结构七-S-CL-003"
        Dim b_PgdMes_gx As String = "02.03.02.048"
        Dim lxxx As String = "02.03.02.047"
        Dim rtn_mes_bg_ng As String = mesbgNg("1", b_PgdMes_gx, b_PgdMesProjectCode, lxxx)
        Dim xxx As String = rtn_mes_bg_ng
    End Sub

End Class

Public Class entity_lx_ck
    Public sys_id As String
    Public factory_id As String
    Public area As String
    Public location As String
    Public prod_id As String
    Public b_pgd_mes_sys_id As String
    Public status As String
    Public into_ope As String
End Class